package com.web;

import com.domain.Activity;
import com.service.activityService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
/**
 * 更新活动的信息
 * 接收 updateActivity.jsp 页面的表单数据，封装成Activity对象
 * 提交到 activityService 的 updateActivity 函数
 * 重定向到 /findAllAct
 */
@WebServlet("/updateActivity")
public class updateActivityServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.解决乱码问题
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //2.获取修改后的表单 ,活动数据封装成map集合
        Map<String, String[]> map = request.getParameterMap();
        //3.对修改后的活动
        Activity activity = new Activity();
        try {
            BeanUtils.populate(activity,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("修改 活动id为"+activity.getActivityId()+"的活动");
        System.out.println("改为:"+activity.toString());
        //4.调用activityService层的活动进行保存
        Integer integer = new activityService().updateActivity(activity);
        System.out.println(integer==1?"修改成功":"修改失败");
        //5.重定向到查询所有活动
        response.sendRedirect(request.getContextPath()+"/findAllAct");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
